﻿using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Globalization;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;

namespace NCS.IConnect.Core.WebApi.Validation
{
     /// <summary>
    /// Validate a string name and ensure it is a valid culture name
    /// </summary>
    [AttributeUsage( AttributeTargets.Property| AttributeTargets.Parameter|  AttributeTargets.Field)]
    [ComVisible(false)]
    public class CultureNameAttribute : ValidationAttribute
    {
        private static IEnumerable<string> allCultures = CultureInfo.GetCultures(CultureTypes.AllCultures).Select(c => c.Name);

        /// <summary>
        /// Determines whether the specified value of the object is valid.
        /// </summary>
        /// <param name="value">The value of the object to validate.</param>
        /// <returns>true if the specified value is valid; otherwise, false.</returns>
        public override bool IsValid(object value)
        {
            string value1 = value as string;
            if (value1 == null)
            {
                return true;
            }
            return allCultures.Contains(value1, StringComparer.OrdinalIgnoreCase);
        }
    }
}
